package demo;

import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    private Queue<Integer> s1 ;
    private Queue<Integer> s2 ;
    public MyStack() {
        s1 = new LinkedList<>();
        s2 = new LinkedList<>();
    }
    
    public void push(int x) {
        if(s2.isEmpty()) {
            s1.add(x);
        }else {
            s2.add(x);
        }
    }
    
    public int pop() {
        if (!empty()) {
            if (!s1.isEmpty()) {
                int size = s1.size();
                while (size - 1 != 0) {
                    int n = s1.poll();
                    s2.add(n);
                    size--;
                }
                return s1.poll();
            } else {
                int size = s2.size();
                while (size - 1 != 0) {
                    int n = s2.poll();
                    s1.add(n);
                    size--;
                }
                return s2.poll();
            }
        }
        return -1;
    }
    
    public int top() {
        if(!empty()) {
            int n = 0;
            if (!s1.isEmpty()) {
                int size = s1.size();
                while (size  != 0) {
                     n = s1.poll();
                    s2.add(n);
                    size --;
                }
            } else {
                int size = s2.size();
                while (size != 0) {
                    n = s2.poll();
                    s1.add(n);
                    size --;
                }
            }
            return n;
        }
        return -1;
    }
    
    public boolean empty() {
        if(s1.isEmpty() && s2.isEmpty()) {
            return true;
        }
        return false;
    }
}